package fun.easycode.webterminal.service.impl;

import fun.easycode.webterminal.common.contract.DynamicOperate;
import fun.easycode.webterminal.common.contract.PageDTO;
import fun.easycode.webterminal.common.contract.R;
import fun.easycode.webterminal.dao.MachineGroup;
import fun.easycode.webterminal.dao.MachineGroupRepository;
import fun.easycode.webterminal.service.MachineGroupService;
import fun.easycode.webterminal.service.cmd.MachineGroupPageQry;
import fun.easycode.webterminal.service.cmd.MachineGroupSaveCmd;
import fun.easycode.webterminal.service.cmd.MachineGroupSaveCmdAssembler;
import fun.easycode.webterminal.service.dto.MachineGroupDTO;
import fun.easycode.webterminal.service.dto.MachineGroupDTOAssembler;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class MachineGroupServiceImpl implements MachineGroupService {

    @Resource
    private MachineGroupRepository machineGroupRepository;

    @Override
    public R<MachineGroupDTO> getById(String id) {
        MachineGroup machineGroup = machineGroupRepository.getById(id);
        if(machineGroup == null){
            return R.error("未找到该分组");
        }
        return R.success(MachineGroupDTOAssembler.INSTANCE.to(machineGroup));
    }

    public R<PageDTO<MachineGroupDTO>> page(MachineGroupPageQry cmd) {
        cmd.validate();
        return R.success(DynamicOperate.page(cmd, machineGroupRepository.getBaseMapper(), MachineGroupDTOAssembler.INSTANCE::to));
    }

    public R<String> save (MachineGroupSaveCmd cmd) {
        cmd.validate();

        machineGroupRepository.saveOrUpdate(MachineGroupSaveCmdAssembler.INSTANCE.to(cmd));
        return R.message("保存成功!");
    }

    @Override
    public R<String> delete(String id) {
        boolean r = machineGroupRepository.removeById(id);
        return r ? R.message("删除成功") : R.error("删除失败");
    }
}
